package cn.ltgodm.template.web.controller.auth;

import cn.ltgodm.template.common.annotation.Log;
import cn.ltgodm.template.common.constant.Constants;
import cn.ltgodm.template.common.domain.AjaxResult;
import cn.ltgodm.template.common.domain.entity.LoginUser;
import cn.ltgodm.template.common.domain.vo.UserVO;
import cn.ltgodm.template.common.enums.BusinessType;
import cn.ltgodm.template.common.utils.SecurityUtils;
import cn.ltgodm.template.user.domain.param.LoginParam;
import cn.ltgodm.template.user.service.UserService;
import cn.ltgodm.template.user.service.impl.LoginService;
import cn.ltgodm.template.user.service.impl.PermissionService;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @author ltgodm
 * @date 2024-03-18 21:01:25
 */
@RestController
@Tag(name = "用户登录")
@ApiSupport(author = "ltgodm")
@RequestMapping("/auth")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private PermissionService permissionService;
    @PostMapping("/login")
    @Operation(summary = "登录",description = "用户登录")
    public AjaxResult login(@RequestBody LoginParam loginParam){
        AjaxResult ajax = AjaxResult.success();
        String token = loginService.login(loginParam.getUsername(),loginParam.getPassword(),loginParam.getCode(),loginParam.getUuid());
        ajax.put(Constants.TOKEN,token);
        return ajax;
    }

    @GetMapping
    @Operation(summary = "获取登录用户信息",description = "获取当前登录用户的信息")
    public AjaxResult getLoginUser(){
        UserVO user = SecurityUtils.getLoginUser().getUser();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        //获取用户角色和权限
        Set<String> rolePermission = permissionService.getRolePermission(user);
        ajax.put("roles", rolePermission);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        ajax.put("permissions", permissions);
        return ajax;
    }


}
